from flask import Blueprint, request, render_template, redirect, session
from models import *

blue = Blueprint("blue", __name__)

'''=========================================================添加分类================================================'''


@blue.route("/addcate", methods=["GET", "POST"])
def addcate():
    if request.method == "GET":
        return render_template("addcate.html")
    else:
        name = request.form.get("name")
        name1 = Cate(name=name)
        db.session.add(name1)
        db.session.commit()
        return {"msg": "添加成功"}


'''=========================================================添加商品================================================'''


@blue.route("/addgoods", methods=["GET", "POST"])
def addgoods():
    cates = Cate.query.all()
    if request.method == "GET":
        return render_template("addgoods.html", cates=cates)
    else:
        name = request.form.get("name")
        price = request.form.get("price")
        cate_id = request.form.get("cate_id")
        print(name)
        print(price)
        print(cate_id)
        goods = Goods(name=name, price=price, cate_id=cate_id)
        db.session.add(goods)
        db.session.commit()
        return {"msg": "添加成功"}


'''=========================================================分类展示================================================'''

from flask_sqlalchemy import Pagination
@blue.route("/catelist")
def cetelist():
    pag_num = int(request.args.get("pag_num", 1))
    per_num = 3
    pagination = Cate.query.paginate(pag_num, per_num)
    return render_template("catelist.html", pages=pagination)

'''=========================================================商品展示================================================'''

from flask_sqlalchemy import Pagination
@blue.route("/goodslist")
def goodslist():
    id = request.args.get("id")
    cates = Cate.query.filter(Cate.id == id).first()
    return render_template("goodslist.html", cates=cates)
